package com.ycz.sas.jgen.handler;


import java.util.HashMap;
import java.util.Map;

import com.sshtools.j2ssh.configuration.SshConnectionProperties;

import net.sf.jftp.config.Settings;
import net.sf.jftp.net.BasicConnection;
import net.sf.jftp.net.ConnectionHandler;
import net.sf.jftp.net.ConnectionListener;
import net.sf.jftp.net.FtpConnection;
import net.sf.jftp.net.wrappers.SftpConnection;

public class FtpOprate implements ConnectionListener{

	private boolean isThere = false;
	private ConnectionHandler handler = new ConnectionHandler();
	private Map connectPool = new HashMap();
	private Settings settings = new Settings();

	@SuppressWarnings("static-access")
	public BasicConnection initConnection(String serverType, String host,
			int port, String user, String password){
		settings.setProperty("jftp.disableLog", true);
		if(serverType.equalsIgnoreCase("ftp")){
			FtpConnection con = new FtpConnection(host, port, "/");
			con.addConnectionListener(this);
			con.setConnectionHandler(handler);
			con.login(user, password);
			while(!isThere){
				try {
					Thread.sleep(10);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

			if(isThere)
				return con;
			else
				return null;
		}
		else
		{
			SftpConnection scon = null;
			SshConnectionProperties pro = new SshConnectionProperties();
			pro.setHost(host);
			pro.setPort(port);
			scon = new SftpConnection(pro);
			scon.addConnectionListener(this);
			if(scon.login(user, password))
				return scon;
			else
				return null;
		}

	}

	@SuppressWarnings("unchecked")
	public BasicConnection getConnection(String serverType, String host,
			int port, String user, String password){
		if(connectPool.isEmpty()){
			BasicConnection tmp = initConnection(serverType, host, port, user, password);
			if(tmp == null || !tmp.isConnected())
				return null;
			else {
				connectPool.put(host, tmp);
				return tmp;
			}
		}
		else
		{
			BasicConnection connection = (BasicConnection) connectPool.get(host);
			if(connection != null){
				return connection;
			} else {
				BasicConnection tmp = this.initConnection(serverType, host, port, user, password);
				if(tmp == null || !tmp.isConnected()){
					return null;
				} else {
					connectPool.put(host, tmp);
					return tmp;
				}
			}
		}
	}

	public boolean ftpUpload(BasicConnection con, String srcDir, String destDir, String file){
		if(con == null)
			return false;
		String path = "";
		String[] paths = destDir.split("/");
		for(String pa : paths){
			path += "/" + pa;
			if(!con.chdir(path))
				con.mkdir(path);
		}
		con.chdir(destDir);
		con.setLocalPath(srcDir);
		int flag = con.upload(file);
		return flag == 1;
	}

	public boolean ftpDownload(BasicConnection con, String srcDir, String destDir, String file){
		if(con == null)
			return false;
		String path = "";
		String[] paths = srcDir.split("/");
		for(String pa : paths){
			path += "/" + pa;
			if(!con.chdir(path))
				con.mkdir(path);
		}
		con.chdir(srcDir);
		con.setLocalPath(destDir);
		int flag = con.download(file);
		return flag == 1;
	}

	public boolean sftpUpload(BasicConnection con, String destination,
			String source, String file) {

		if (con == null) {
			return false;
		} else {
			String path = "";
			String[] paths = destination.split("/");
			for(String pa : paths){
				path += "/" + pa;
				if(!con.chdir(path))
					con.mkdir(path);
			}
			con.chdir(destination);
			con.setLocalPath(source);
			int flag = con.upload(file);
			return flag == 1;
		}
	}

	public void release(){
		try {
			if (connectPool != null && !connectPool.isEmpty()) {
				Object[] obj = connectPool.values().toArray();
				for (int i = 0; i < obj.length; i++) {
					BasicConnection connect = (BasicConnection) obj[i];
					if (connect != null && connect.isConnected()) {
						connect.disconnect();
					}
				}
				connectPool.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionFinished(BasicConnection arg0) {
		System.out.println("action finished");
	}

	@Override
	public void connectionFailed(BasicConnection arg0, String arg1) {
		System.out.println("connection failed!");
	}

	@Override
	public void connectionInitialized(BasicConnection arg0) {
		isThere = true;
	}

	@Override
	public void updateProgress(String file, String type, long bytes) {
	}

	@Override
	public void updateRemoteDirectory(BasicConnection con) {
		System.out.println("new path is: " + con.getPWD());
	}

	public static void main(String[] args) {
		FtpOprate ftp = new FtpOprate();
		BasicConnection con = ftp.getConnection("ftp", "172.16.12.30", 21, "root", "www.mykingdee.com");
		ftp.ftpUpload(con, "D:/2/", "/easapp/eas71test/eas/server/lib/sp", "1.sh");
//		ftp.ftpDownload(con, "/easapp/eas71test/eas/server/lib/sp/", "D:/2/", "eas_sp.jar");
		ftp.release();
	}

}
